package app.controllers.admin.game;

import app.Const;
import app.models.DaoKit;
import app.models.game.*;
import com.github.sog.controller.BasicController;
import com.github.sog.controller.datatables.core.DatatablesCriterias;
import com.github.sog.libs.AppFunc;
import com.github.sog.plugin.sqlinxml.SqlKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * .
 * </p>
 *
 * @author Jerry Ou
 * @version 1.0 2014-04-13 11:04
 * @since JDK 1.6
 */
public class GameController extends BasicController {

    public void index() {
        render("index.ftl");
    }

    public void list() {
        final DatatablesCriterias criterias = getCriterias();
        Page<Game> page = Game.dao.datatable(criterias);

        renderDataTables(page, criterias);
    }

    public void create() {
        Game game = new Game();

        List<GameCategory> categories = GameCategory.dao.list();
        List<GameAwards> awardses = GameAwards.dao.list();
        List<GameRule> rules = GameRule.dao.list();

        setAttr("oper", "create");
        setAttr("categories", categories);
        setAttr("awardses", awardses);
        setAttr("rules", rules);
        setAttr("game", game);
        render("item.ftl");
    }

    public void edit() {
        String id = getPara();
        Game game = Game.dao.findById(id);

        List<GameCategory> categories = GameCategory.dao.list();
        List<GameAwards> awardses = GameAwards.dao.list();
        //List<Record> awardses = GameAwards.dao.findWithRfByGameId(id);
        List<GameRule> rules = GameRule.dao.list();

        List<GamerfAwards> rfs = GamerfAwards.dao.findRfByGame(id);
        if (rfs != null && !rfs.isEmpty()) {
            for (GameAwards ga : awardses) {
                int gaid = ga.getInt("id");
                for(GamerfAwards rf : rfs) {
                    int aid = rf.getInt("awards");
                    if (gaid == aid) {
                        ga.put("selected", "1");
                    }
                }
            }
        }

        setAttr("oper", "edit");
        setAttr("categories", categories);
        setAttr("awardses", awardses);
        setAttr("rules", rules);
        setAttr("game", game);
        render("item.ftl");
    }

    public void save() {
        Game game = getModel(Game.class);
        if (DaoKit.isNew(game)) {
            game.set("create_time", new Date());
            game.save();
        } else {
            game.update();
        }

        int gameId = game.getInt("id");
        GamerfAwards.dao.deleteByGame(gameId);

        Integer[] gameAwards = getParaValuesToInt("gameAwards");

        if (gameAwards != null) {
            for (Integer awards : gameAwards) {
                GamerfAwards ga = new GamerfAwards();
                ga.set("game",gameId);
                ga.set("awards",awards);
                ga.save();
            }
        }

        redirect("/admin/game/game");
    }

    public void remove() {
        String remove_ids = getPara(Const.DATA_PARAM);
        List<String> ids = AppFunc.COMMA_SPLITTER.splitToList(remove_ids);
        if (DaoKit.batch_remove(SqlKit.sql("game.remove"), ids)) {
            renderSuccess();
        } else {
            renderError("删除出现错误");
        }
    }

    public void state() {
        long id = getParaToLong("id", 0l);
        boolean state = getParaToBoolean("state", false);

        Db.update(SqlKit.sql("game.updateState"), !state, id);
        renderSuccess();
    }

}
